<?php

namespace App\Http\Controllers;
use App\Goods;
use App\GoodsColors;
use App\Goodsfirst;
use App\Goodssecond;
use App\GoodsSizes;
use App\Part;
use App\Pic;
use App\Sqlgoods;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\GoodsTask;
use App\Goodsthird;
use Excel;
use Illuminate\Support\Facades\Storage;
use PHPExcel_IOFactory;
use PHPExcel_Cell;
use App\Rule;
use DB;
class GoodsController extends Controller
{
    public function getAll($company) {
        $goods=Goods::where('company',$company)->get();
        return response()->json($goods);
    }


    public function getColors($id) {
        $colors=Goods::find($id)->colors;
        return response()->json($colors);
    }
    public function getSizes($id) {
        $sizes=Goods::find($id)->sizes;
        return response()->json($sizes);
    }
    public function getPics($id) {
        $pics=Pic::find($id)->pics;
        return response()->json($pics);
    }
    public function getCurrent($page,$company) {
        $pages=$page*10-10;
        $goods=Goods::where('company',$company)->orderBy('id','desc')->skip($pages)->take(10)->get();
        return response()->json($goods);
    }
    public function one($id) {
        $goods=Goods::find($id);
        return response()->json($goods);
    }

    public function getCount($company) {
        $goods=Goods::where('company',$company)->count();
        return response()->json($goods);
    }
    public function add( Request $request) {
        $imgs=$request->input('allUploadImg');


        $addGoods= $input=$request->input('goods');
        $addGoods['code']='code'.time().rand(100,999);
        $addGoods['color']= implode(',',$input['colors']);
        $addGoods['size']= implode(',',$input['sizes']);
        $i=1;
        foreach ($imgs as $img){
            if($i== 1){
                $addGoods['img']=$img;
            }elseif($i == 2){
                $addGoods['img1']=$img;
            }elseif(($i == 3)){
                $addGoods['img2']=$img;
            }else{
                break;
            }
            $i=$i+1;
        }
        $goods=Goods::create($addGoods);
        foreach ($input['colors'] as $color){
            $goodsColor=new GoodsColors(['color'=>$color]);
            $goods->colors()->save($goodsColor);
        }
        foreach ($input['sizes'] as $size){
            $goodsSize=new GoodsSizes(['size'=>$size]);
            $goods->sizes()->save($goodsSize);
        }
        foreach ($imgs as $img){
            $goodsPic=new Pic(['url'=>$img]);
            $goods->pics()->save($goodsPic);
        }
        if($goods){
            return response()->json('添加成功');
        }else{
            return response()->json('添加失败');
        }

    }
    public function del ( Request $request) {
        $date=$request->input('date');

        foreach ($date as $d){
            Goods::destroy($d['id']);
        }
        return response()->json('删除成功');

    }
    public function inputPost ( Request $request) {

        if ($request->hasFile('inputGoods')) {
            $file = $request->file('inputGoods');
            $entension = $file->getClientOriginalExtension(); //上传文件的后缀.
            $name = date('ymdhis')."." . $entension;
            $request->file('inputGoods')->move('public', $name);


        }
        Excel::load('public/'.$name, function($reader) {

            $data= $reader->all();
            dd($data);
            foreach ($data as $d){
                $input['name']=$d[1];
                $input['goodsCode']=$d[0];
                $input['goodsType']=$d[2];
                $input['goodsUnit']=$d[3];
                $input['goodsNotes']=$d[4];
                if($d[5] == 1){
                    Goodsfirst::create($input);
                }elseif($d[5] == 2){
                    Goodssecond::create($input);
                }elseif($d[5] == 3){
                    Goodsthird::create($input);
                }
            }

        });

    }
    public function update($id, Request $request) {

        $imgs=$request->input('allUploadImg');
        $colors=$request->input('colors');
        $sizes=$request->input('sizes');
        $editGoods= $input=$request->input('goods');
        $editGoods['color']= implode(',',$colors);
        $editGoods['size']= implode(',',$sizes);
        $i=1;
        foreach ($imgs as $img){
            if($i== 1){
                $editGoods['img']=$img;
            }elseif($i == 2){
                $editGoods['img1']=$img;
            }elseif(($i == 3)){
                $editGoods['img2']=$img;
            }else{
                break;
            }
            $i=$i+1;
        }
        $goods=Goods::find($id);
        $goods->colors()->delete();
        $NewGoods=Goods::where('id',$id)->update($editGoods);
        foreach ($colors as $color){
            $goodsColor=new GoodsColors(['color'=>$color]);
            $NewGoods->colors()->save($goodsColor);
        }
        $goods->sizes()->delete();
        foreach ($sizes as $size){
            $goodsSize=new GoodsSizes(['size'=>$size]);
            $NewGoods->sizes()->save($goodsSize);
        }
        $pics=$goods->pics();
        foreach ($pics as $pic){
            Storage::delete($pic->url);
        }
        $goods->pics()->delete();
        foreach ($imgs as $img){
            $goodsPic=new Pic(['url'=>$img]);
            $NewGoods->pics()->save($goodsPic);
        }
    }
    public function search($word) {
        $goods=Goods::where('name','like','%'.$word.'%')->OrWhere('email','like','%'.$word.'%')->get();
        return response()->json($goods);
    }
    public function page($page,$pagesize) {
        $skip=$page*$pagesize;
        $take=$pagesize;
        $goods=Goods::orderBy('id','desc')->skip($skip)->take($take)->get();
        return response()->json($goods);
    }
    public function dateOne($date) {

        $goods=Goods::where('created_at',$date)->get();
        return response()->json($goods);
    }
    public function datemore($date) {

        $goods=Goods::where('created_at',$date)->get();
        return response()->json($goods);
    }
}
